package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Customer;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Result;

import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pojo.pro.Workflow;
import com.qidianit.mes.pro.mapper.ProductionOrderMapper;
import com.qidianit.mes.pro.mapper.WorkflowMapper;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.ProductionOrderService;
import com.qidianit.mes.sys.mapper.CustomerMapper;
import com.qidianit.mes.sys.mapper.DictDataMapper;
import com.qidianit.mes.sys.mapper.MaterialMapper;
import com.qidianit.mes.sys.mapper.UnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Objects;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/17 0017 - 16:10
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private WorkflowProductMapper workflowProductMapper;
    @Autowired
    private WorkflowMapper workflowMapper;
    @Override
    public Result<PageInfo<ProductionOrder>> selectList(ProductionOrder productionOrder,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<ProductionOrder> productionOrders = productionOrderMapper.selectList(productionOrder);
        for (ProductionOrder order : productionOrders) {
            order.setTypeDictData(dictDataMapper.selectByType(order.getProductionOrderType()));
            order.setStatusDictData(dictDataMapper.selectByStatus(order.getStatus()));
            order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
            Material material = materialMapper.selectByCodeMaterial(order.getMaterialCode());
            order.setMaterial(material);
            material.setUnit(unitMapper.selectByIdUnit(material.getUnitId()));
        }
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<ProductionOrder> selectById(Long id) {
        ProductionOrder productionOrder = productionOrderMapper.selectById(id);
        productionOrder.setTypeDictData(dictDataMapper.selectByType(productionOrder.getProductionOrderType()));
        productionOrder.setStatusDictData(dictDataMapper.selectByStatus(productionOrder.getStatus()));
        productionOrder.setCustomer(customerMapper.selectByCode(productionOrder.getCustomerCode()));
        productionOrder.setMaterial(materialMapper.selectByCodeMaterial(productionOrder.getMaterialCode()));
        return Result.success(productionOrder);
    }

    @Override
    public Result insertProductionOrder(ProductionOrder productionOrder) {
        if (productionOrder.getProductionOrderCode() == null ||"".equals(productionOrder.getProductionOrderCode()) ||
                productionOrder.getProductionOrderName() == null || "".equals(productionOrder.getProductionOrderName()) ||
                productionOrder.getProductionOrderSource() == null ||
                productionOrder.getProductionOrderType() == null ||"".equals(productionOrder.getProductionOrderType()) ||
                productionOrder.getOrderCode() == null || "".equals(productionOrder.getOrderCode()) ||
                productionOrder.getMaterialCode() == null || "".equals(productionOrder.getMaterialCode()) ||
                productionOrder.getProductionOrderQuantity() == null ||
                productionOrder.getCustomerCode() == null || "".equals(productionOrder.getCustomerCode()) ||
                productionOrder.getRequestDate() == null ||
                productionOrder.getQrCodeUrl() == null || "".equals(productionOrder.getQrCodeUrl())
        ){
            return Result.error("添加失败，添加信息不完整");
        }
        ProductionOrder productionOrder1 = productionOrderMapper.selectByProductionOrderCode(productionOrder.getProductionOrderCode());
        if(productionOrder1 != null){
            return Result.error("工单编号重复");
        }
        return Result.success(productionOrderMapper.insertProductionOrder(productionOrder));
    }

    @Override
    public Result updateById(ProductionOrder productionOrder) {
        if (productionOrder.getProductionOrderCode() == null ||"".equals(productionOrder.getProductionOrderCode()) ||
                productionOrder.getProductionOrderName() == null || "".equals(productionOrder.getProductionOrderName()) ||
                productionOrder.getProductionOrderSource() == null ||
                productionOrder.getProductionOrderType() == null ||"".equals(productionOrder.getProductionOrderType()) ||
                productionOrder.getOrderCode() == null || "".equals(productionOrder.getOrderCode()) ||
                productionOrder.getMaterialCode() == null || "".equals(productionOrder.getMaterialCode()) ||
                productionOrder.getProductionOrderQuantity() == null ||
                productionOrder.getCustomerCode() == null || "".equals(productionOrder.getCustomerCode()) ||
                productionOrder.getRequestDate() == null ||
                productionOrder.getStatus() == null || "".equals(productionOrder.getStatus()) ||
                productionOrder.getQrCodeUrl() == null || "".equals(productionOrder.getQrCodeUrl())
        ){
            return Result.error("编辑工单失败");
        }
        Material material = materialMapper.selectByCodeMaterial(productionOrder.getMaterialCode());
        if (material == null){
            return Result.error("物料编码或客户编码不存在");
        }
        Customer customer = customerMapper.selectByCode(productionOrder.getCustomerCode());
        if (customer == null){
            return Result.error("物料编码或客户编码不存在");
        }
        ProductionOrder productionOrder1 = productionOrderMapper.selectByProductionOrderCode(productionOrder.getProductionOrderCode());
        if(productionOrder1 != null && !Objects.equals(productionOrder1.getId(), productionOrder.getId())){
            return Result.error("工单编号重复");
        }
        return Result.success(productionOrderMapper.updateById(productionOrder));
    }

    @Override
    public Result deleteById(Long id) {
        return productionOrderMapper.deleteById(id)==1?Result.success():Result.error("删除工单失败");
    }


    @Override
    public Result<ProductionOrder> selectByProductionOrderCode(String productionOrderCode) {
        ProductionOrder productionOrder = productionOrderMapper.selectByProductionOrderCode(productionOrderCode);
        Material material = materialMapper.selectByCodeMaterial(productionOrder.getMaterialCode());
        productionOrder.setMaterial(material);
        material.setUnit(unitMapper.selectByIdUnit(material.getUnitId()));
        return Result.success(productionOrder);
    }

    @Override
    public Result<PageInfo<ProductionOrder>> selectListStatus(ProductionOrder productionOrder, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<ProductionOrder> productionOrders = productionOrderMapper.selectListStatus(productionOrder);
        for (ProductionOrder order : productionOrders) {
            order.setTypeDictData(dictDataMapper.selectByType(order.getProductionOrderType()));
            order.setStatusDictData(dictDataMapper.selectByStatus(order.getStatus()));
            order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
            order.setMaterial(materialMapper.selectByCodeMaterial(order.getMaterialCode()));
        }
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<ArrayList<Workflow>> selectWorkflowById(Long id) {
        Material material = materialMapper.selectMaterialCodeById(id);
        Long[] longs = workflowProductMapper.selectWorkflowIdByProductCode(material.getCode());
        ArrayList<Workflow> workflows = workflowMapper.selectWorkflowById(longs);
        return Result.success(workflows);
    }

    @Override
    public Result updateStatusById(Long id) {
        return Result.success(productionOrderMapper.updateStatusById(id));
    }

    @Override
    public Result updateStatusById2(Long id) {
        return Result.success(productionOrderMapper.updateStatusById2(id));
    }

    @Override
    public Result updateStatusById3(Long id) {
        return Result.success(productionOrderMapper.updateStatusById3(id));    }


}
